package start.spring.basic.flowable.controller.deploy;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.security.annotation.RequiresPermissions;

import start.spring.basic.flowable.controller.BaseController;
import start.spring.basic.flowable.enums.flowable.DeployStatus;
import start.spring.basic.flowable.service.deploy.IWfDeployService;
import start.spring.basic.flowable.vo.flowable.ProcessQuery;
import start.spring.basic.flowable.vo.flowable.WfDeployVo;
import start.spring.basic.page.PageQuery;
import start.spring.basic.response.ResponseData;
import start.spring.basic.response.ResponsePage;

/**
 * 流程部署
 *
 * @author KonBAI
 * @createTime 2022/3/24 20:57
 */
@RestController
@RequestMapping("/deploy")
public class WfDeployController extends BaseController {

	@Autowired
    private IWfDeployService deployService;
	
//	@Autowired
//    private IWfDeployFormService deployFormService;

    /**
     * 查询流程部署列表
     */
    @PostMapping("/list")
    public ResponsePage<WfDeployVo> list(PageQuery pageQuery,@RequestBody ProcessQuery processQuery) {
        return super.returnResponse(deployService.queryPageList(pageQuery, processQuery));
    }

    /**
     * 查询流程部署版本列表
     */
    @PostMapping("/publishList")
    public ResponsePage<WfDeployVo> publishList(PageQuery pageQuery,@RequestBody ProcessQuery processQuery) {
        return super.returnResponse(deployService.queryPublishList(pageQuery, processQuery.getProcessKey()));
    }

    /**
     * 激活或挂起流程
     *
     * @param state 状态（active:激活 suspended:挂起）
     * @param definitionId 流程定义ID
     */
	@RequiresPermissions("workflow:deploy:edit")
    @GetMapping(value = "/changeState")
    public ResponseData<Void> changeState(@RequestParam String state, @RequestParam String definitionId) {
        if(!DeployStatus.statusValid(state)) {
        	return ResponseData.errorMsg("请传入正确的部署状态！");
        }else {
        	deployService.updateState(definitionId, state);
            return ResponseData.successMsg(DeployStatus.getRemark(state) + "成功！");
        }
    }

    /**
     * 读取xml文件
     * @param definitionId 流程定义ID
     * @return
     */
    @GetMapping("/bpmnXml/{definitionId}")
    public ResponseData<String> getBpmnXml(@PathVariable(value = "definitionId") String definitionId) {
        return ResponseData.successData(deployService.queryBpmnXmlById(definitionId));
    }

    /**
     * 删除流程模型
     * @param deployIds 流程部署ids
     */
	@RequiresPermissions("workflow:deploy:remove")
    @PostMapping("/remove")
    public ResponseData<String> remove(@RequestBody List<String> deployIds) {
        deployService.deleteByIds(deployIds);
        return ResponseData.successMsg("已删除所选模型部署！");
    }

//    /**
//     * 查询流程部署关联表单信息
//     *
//     * @param deployId 流程部署id
//     */
//    @GetMapping("/form/{deployId}")
//    public R<?> start(@PathVariable(value = "deployId") String deployId) {
//        WfForm formVo = deployFormService.selectDeployFormByDeployId(deployId);
//        if (Objects.isNull(formVo)) {
//            return R.fail("请先配置流程表单");
//        }
//        return R.ok(JSONObject.parseObject(formVo.getContent(), Map.class));
//    }
}
